﻿using AG.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using AG.MapEditor.Data;

namespace AGMapEditor.Windows
{
    public partial class AGSelectMapWindow : Form
    {
        public AGMap SelectedMap { get; private set; }
        private AGMap _curMap;

        public AGSelectMapWindow(AGMap curMap)
        {
            InitializeComponent();

            _curMap = curMap;
            BindMapList();
        }


        private void BindMapList()
        {
            string mapFolder = AppDomain.CurrentDomain.BaseDirectory + "data\\maps";
            if (!Directory.Exists(mapFolder))
            {
                return;
            }

            string[] files = Directory.GetFiles(mapFolder, "*.map");
            for (int index = 0; index < files.Length; index++)
            {
                MapFileInfo item = new MapFileInfo();
                FileInfo fileInfo = new FileInfo(files[index]);
                item.MapFileName = fileInfo.Name;
                item.MapFileFullName = fileInfo.FullName;
                item.MapId = Convert.ToInt16(fileInfo.Name.Replace(fileInfo.Extension, string.Empty));
                if (item.MapId != _curMap.Id)
                {
                    _ctlListMap.Items.Add(item);
                }
            }
        }

        private void _ctlBtnOK_Click(object sender, EventArgs e)
        {
            MapFileInfo selectedMapInfo = _ctlListMap.SelectedItem as MapFileInfo;
            if (selectedMapInfo != null)
            {
                SelectedMap = AGMEContext.Current.MapStore.LoadMap(selectedMapInfo.MapFileFullName);
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        class MapFileInfo
        {
            public int MapId { get; set; }
            public string MapFileFullName { get; set; }
            public string MapFileName { get; set; }

            public override string ToString()
            {
                return MapFileName;
            }
        }
    }
}
